package springTaskScheduler.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author CKC
 * @DateTime 2021/12/12 17:02
 */
@Configuration
@EnableScheduling
@EnableAsync
public class TaskSchedulerConfig implements SchedulingConfigurer, AsyncConfigurer, AsyncUncaughtExceptionHandler {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    }

    @Override
    public Executor getAsyncExecutor() {
        return new ThreadPoolExecutor(2, 4, 10L, TimeUnit.SECONDS, new LinkedBlockingDeque<>(10));
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }

    @Override
    public void handleUncaughtException(Throwable ex, Method method, Object... params) {
        String name = method.getName();
        System.out.println("method:" + name + "\t params:" + params + "\tex:" + ex.getMessage());
    }
}
